package com.xbongbong.sys.util;

import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/07/20 14:47
 */
public class UserPermissionUtil {

    /**
     * 判断用户的数据权限
     * 和userVO.isMaxDataPermission是一样的效果
     * @param userVO
     * @param saasMark
     * @param businessType
     * @param distributorMark
     * @param menuId
     * @param isOrganization
     * @return boolean
     * @author 吴峰
     * @date 2022/7/20 14:53
     */
    public static boolean isMaxDataPermission(UserVO userVO, Integer saasMark, Integer businessType, Integer distributorMark, Long menuId, boolean isOrganization) {
        Integer permission = null;
        Map<String, Integer> dataPermissionDetail = userVO.getDataPermissionDetail();
        if (isOrganization) {
            permission = dataPermissionDetail.get(SaasMarkEnum.SAAS.getCode() + StringConstant.UNDER_LINE + XbbRefTypeEnum.SYSTEM.getCode());
        } else {
            SaasMarkEnum saasMarkEnum = SaasMarkEnum.getMemo(saasMark);
            businessType = XbbRefTypeEnum.transferBusinessType4Distributor(businessType, distributorMark);
            //出库发货单没有单独的menu和销售出库单共用
            if (Objects.equals(businessType,XbbRefTypeEnum.ORDER_OUTSTOCK.getCode())){
                businessType = XbbRefTypeEnum.CONTRACT_OUTSTOCK.getCode();
            }
            String key;
            if (!Objects.isNull(saasMarkEnum)) {
                switch (saasMarkEnum) {
                    case SAAS:
                        key = saasMarkEnum.getCode() + StringConstant.UNDER_LINE + businessType;
                        permission = dataPermissionDetail.get(key);
                        break;
                    case PAAS:
                        key = saasMarkEnum.getCode() + StringConstant.UNDER_LINE + menuId;
                        permission = dataPermissionDetail.get(key);
                        break;
                    case THIRD_PARTY:
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + saasMarkEnum);
                }
            }
        }
        return permission != null && permission.equals(5);
    }
}
